package com.siyoumi.modules.accsuper_admin.service;

import com.siyoumi.component.XApp;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XRedis;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.entity.*;
import com.siyoumi.modules.accsuper_admin.vo.VoSysAccsuperConfig;
import com.siyoumi.modules.accsuper_admin.vo.VoSysAccspuerConfig;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//项目
@Slf4j
@Service
public class SvcSysAccspuerConfig {
    static public SvcSysAccspuerConfig getBean() {
        return XSpringContext.getBean(SvcSysAccspuerConfig.class);
    }

    static public SysAccsuperConfigService getApp() {
        return SysAccsuperConfigService.getBean();
    }

    public SysAccsuperConfig getEntityByAppId(String appId) {
        String key = getApp().getEntityCacheKey(appId);
        return XRedis.getBean().getAndSetData(key, k -> {
            JoinWrapperPlus<SysAccsuperConfig> query = getApp().join();
            query.eq("aconfig_app_id", appId);
            return getApp().first(query);
        }, SysAccsuperConfig.class);
    }

    /**
     * 根据类型获取api配置
     *
     * @param type
     */
    public SysAccsuperZzzApi getApiEntity(String type) {
        if (XStr.isNullOrEmpty(type)) {
            return null;
        }
        String key = SysAccsuperZzzApiService.getBean().getEntityCacheKey(type);
        return XRedis.getBean().getAndSetData(key, k -> {
            JoinWrapperPlus<SysAccsuperZzzApi> query = SysAccsuperZzzApiService.getBean().join();
            query.eq("api_type", type)
                    .eq("api_x_id", XHttpContext.getX());
            return SysAccsuperZzzApiService.getBean().first(query);
        }, SysAccsuperZzzApi.class);
    }

    public XReturn addAccsuper(VoSysAccsuperConfig vo, Boolean wxopen) {
        SysAccsuperConfig entity = XApp.getTransaction().execute(status -> {
            SysAccsuperConfig entityConfig = getApp().first(vo.getAcc_uid());

            log.debug("开始保存");
            SysAccsuperConfig entityConfigUpdate = new SysAccsuperConfig();
            entityConfigUpdate.setAconfig_id(vo.getAcc_uid());
            entityConfigUpdate.setAconfig_x_id(entityConfigUpdate.getAconfig_id());
            entityConfigUpdate.setAconfig_type(vo.getAconfig_type());
            entityConfigUpdate.setAconfig_token(XApp.getUUID());
            if (wxopen) {
                log.debug("开放平台授权");
                entityConfigUpdate.setAconfig_wxopen(1);
            }
            getApp().saveOrUpdatePassEqualField(entityConfig, entityConfigUpdate);

            if (entityConfig == null) {
                log.debug("保存超管登陆账号");
                SysAccountService appAcc = SysAccountService.getBean();
                SysAccount entityAcc = appAcc.loadEntity(XBean.toMap(vo));
                entityAcc.setAcc_role("super_admin");
                entityAcc.setAcc_x_id(entityConfigUpdate.getAconfig_id());
                entityAcc.setAcc_pwd(XApp.encPwd(entityConfigUpdate.getAconfig_id(), vo.getAcc_pwd()));
                entityAcc.setAutoID();
                appAcc.save(entityAcc);

                log.debug("保存开发者账号");
                SysAccount entityAccDev = new SysAccount();
                entityAccDev.setAcc_role("dev");
                entityAccDev.setAcc_uid("dev");
                entityAccDev.setAcc_name("开发者");
                entityAccDev.setAcc_x_id(entityAcc.getAcc_x_id());
                //当前时间年月日
                entityAccDev.setAcc_pwd(XApp.encPwd(entityConfigUpdate.getAconfig_id(), XDate.now().format(XDate.yyyyMMdd_PATTERN)));
                entityAccDev.setAutoID();
                appAcc.save(entityAccDev);
            }

            return entityConfigUpdate;
        });


        XReturn r = XReturn.getR(0);
        r.setData("entity", entity);

        return r;
    }


    public XReturn edit(InputData inputData, VoSysAccspuerConfig vo) {
        List<String> ignoreField = new ArrayList<>();
        if (inputData.isAdminEdit()) {
        }

        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, true, ignoreField);
            SysAccsuperConfig entity = r.getData("entity");
            //清徐缓存
            getApp().delEntityCache(entity.getKey());
            //接口配置
            {
                SysAccsuperZzzApi entityApi = getApiEntity(entity.getAconfig_api_type());
                SysAccsuperZzzApi entityApiUpdate = new SysAccsuperZzzApi();
                if (entityApi == null) {
                    entityApiUpdate.setApi_x_id(entity.getKey());
                    entityApiUpdate.setApi_type(entity.getAconfig_api_type());
                    entityApiUpdate.setAutoID();
                }
                XBean.copyProperties(vo, entityApiUpdate);
                SysAccsuperZzzApiService.getBean().saveOrUpdatePassEqualField(entityApi, entityApiUpdate);
                SysAccsuperZzzApiService.getBean().delEntityCache(vo.getAconfig_api_type());
            }

            return r;
        });
    }
}
